﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MB.RuleBase.Common;
using WoChu.Autorun.Base.Util;
using Newtonsoft.Json;
using System.Web;
using Model;

namespace WoChu.Autorun.Base.Dao
{
    public class Dao
    {
        private string _configFileName;
        public string ConfigFileName
        {
            get { return _configFileName; }
            set { _configFileName = value; }
        }

        public Dao()
        {
            Type type = this.GetType();
            foreach (Attribute attr in Attribute.GetCustomAttributes(type))
            {
                if (attr.GetType() == typeof(ConfigFileNameAttribute))
                {
                    _configFileName = ((ConfigFileNameAttribute)attr).ConfigFileName;
                }
            }
        }

        public string SerializeToJson<T>(T model)
        {
            string strSerializeJSON = "";
            strSerializeJSON = JsonConvert.SerializeObject(model);

            return strSerializeJSON;
        }

        public string SerializeToJson<T>(List<T> modellist)
        {
            string strSerializeJSON = "";
            foreach (T model in modellist)
            {
                strSerializeJSON += JsonConvert.SerializeObject(model);
            }
            return strSerializeJSON;
        }

        public T DeSerializeObject<T>(string SerializeJSON)
        {
            T model = default(T);

            model = (T)JsonConvert.DeserializeObject(SerializeJSON, typeof(T));
            return model;
        }

        public ResultModel GetAPIResult(object ob)
        {
            return new ResultModel { RESULT = "success", MESSAGE = "成功", DATA = ob };
        }

    }
}
